//index.js
let QRCode = require('../../utils/weapp-qrcode.js');
let qrcode;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    placeholder:'http://www.baidu.com',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let size = this.setCanvasSize();
    let url = this.data.placeholder;
    qrcode = new QRCode('canvas', {
      text: url,
      width: size.w,
      height: size.h,
      colorDark: "#000000",
      colorLight: "#ffffff",
      correctLevel: QRCode.CorrectLevel.H,
    });
  },
  //调用canvas插件
  //获取canvas宽高
  setCanvasSize(){
    let size = {};
    let res = wx.getSystemInfoSync();
    //console.log(res);
    let scale =  686/750;
    let width = res.windowWidth*scale;
    let height = width;
    size.w = width;
    size.h = height;
    return size;
  },
  //提交表单，保存url
  fromSubmit(e){
    let _this = this;
    let url = e.detail.value.url || this.data.placeholder;
    wx.showToast({
      title: '生成中...',
      icon:'loading',
      duration:2000
    })

    let time = setTimeout(()=>{
      let size = _this.setCanvasSize();
      qrcode = new QRCode('canvas', {
        text: url,
        width: size.w,
        height: size.h,
        colorDark: "#000000",
        colorLight: "#ffffff",
        correctLevel: QRCode.CorrectLevel.H,
      });
      wx.hideToast();
      clearTimeout(time);
    },2000)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})